package com.tensquare.notice.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.entity.Result;
import com.tensquare.notice.client.ArticleClient;
import com.tensquare.notice.client.UserClient;
import com.tensquare.notice.dao.NoticeDao;
import com.tensquare.notice.dao.NoticeFreshDao;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class NoticeService {

  @Autowired private UserClient userClient;
  @Autowired private ArticleClient articleClient;
  @Autowired private NoticeDao noticeDao;
  @Autowired private NoticeFreshDao noticeFreshDao;
  @Autowired private IdWorker idWorker;

  public Notice selectById(String id) {
    Notice notice = noticeDao.selectById(id);
    getNoticeInfo(notice);
    return notice;
  }

  public Page<Notice> selectByPage(Notice notice, Integer page, Integer size) {
    Page<Notice> pageData = new Page<>(page, size);
    List<Notice> noticeList = noticeDao.selectPage(pageData, new EntityWrapper<>(notice));

    for (Notice n : noticeList) {
      getNoticeInfo(n);
    }

    pageData.setRecords(noticeList);
    return pageData;
  }

  public void save(Notice notice) {
    // 设置初始值
    // 设置状态 0表示未读  1表示已读
    notice.setState("0");
    notice.setCreatetime(new Date());

    // 使用分布式Id生成器，生成id
    String id = idWorker.nextId() + "";
    notice.setId(id);
    noticeDao.insert(notice);

    // 待推送消息入库，新消息提醒
    // NoticeFresh noticeFresh = new NoticeFresh();
    // noticeFresh.setNoticeId(id); // 消息id
    // noticeFresh.setUserId(notice.getReceiverId()); // 待通知用户的id
    // noticeFreshDao.insert(noticeFresh);
  }

  public void updateById(Notice notice) {
    noticeDao.updateById(notice);
  }

  public Page<NoticeFresh> freshPage(String id, Integer page, Integer size) {
    NoticeFresh noticeFresh = new NoticeFresh();
    noticeFresh.setUserId(id);

    Page<NoticeFresh> pageData = new Page<>(page, size);

    List<NoticeFresh> list = noticeFreshDao.selectPage(pageData, new EntityWrapper<>(noticeFresh));

    pageData.setRecords(list);

    return pageData;
  }

  public void freshDelete(NoticeFresh noticeFresh) {
    noticeFreshDao.delete(new EntityWrapper<>(noticeFresh));
  }

  /**
   * 查询消息相关数据
   *
   * @param notice
   */
  private void getNoticeInfo(Notice notice) {
    Result userResult = userClient.findById(notice.getOperatorId());
    HashMap userMap = (HashMap) userResult.getData();
    notice.setOperatorName(userMap.get("nickname").toString());

    if ("article".equals(notice.getTargetType())) {
      Result articleResult = articleClient.findById(notice.getTargetId());
      HashMap articleMap = (HashMap) articleResult.getData();
      notice.setTargetName(articleMap.get("title").toString());
    }
  }
}
